package com.example.myapplication.demo;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Scroller;

import static android.R.attr.startX;

/**
 * 自定义模板
 * <p>
 * Created by ly on 2016/10/14 09:58.
 */

public class MyView extends FrameLayout {

    private Context mContext;
    private Scroller mScroller;

    public MyView(Context context) {
        this(context, null);
    }

    public MyView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        mScroller = new Scroller(mContext);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int mLastX = 0;
        int mLastY = 0;
        int x = (int) event.getX();
        int y = (int) event.getY();
        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:

                break;
            case MotionEvent.ACTION_UP:

                break;
            case MotionEvent.ACTION_MOVE:
                int deltaX = x - mLastX;
                int deltaY = y - mLastY;
                break;
        }
        mLastX = x;
        mLastY = y;
        return true;
    }

    // 与computeScroll()配合使用滑动view


    private void smoothScroll(int destX, int destY) {
        int scrollX = getScrollX();
        int scrollY = getScrollY();
        final int deltaX = destX - scrollX;
        int deltaY = destY - scrollY;
        //startScroll(startX,starY,dX,dY,duration);  dx,dy表示滑动的距离
        mScroller.startScroll(scrollX, scrollY, deltaX, deltaY, 500);
        invalidate();
        dongHua(deltaX);

    }

    private void dongHua(final int deltaX) {
        //属性动画
        ObjectAnimator.ofFloat(this, "translationX", 0, 100).setDuration(1000).start();

        //第二种方式  模仿scroller
        final ValueAnimator animator = ValueAnimator.ofInt(0, 1).setDuration(1000);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float fraction = animator.getAnimatedFraction();
                scrollTo(startX + (int) (deltaX * fraction), 0);
            }
        });
        animator.start();
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            invalidate();
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return super.onInterceptTouchEvent(ev);
    }


    /**
     事件分发原理代码
     * @param event
     * @return
     */
    public boolean dispatchTouchEvent(MotionEvent event) {
        boolean consume = false; //消耗
        if (onInterceptTouchEvent(event)) {
            consume = onTouchEvent(event);
        } else {
            View child = getChildAt(0);
            consume = child.dispatchTouchEvent(event);
        }

        return consume;
    }
}
